package com.situ.day14;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import org.junit.Test;

public class IoResderTest {
	public static void main(String[] args) {
		try {
			FileReader fileReader = new  FileReader("D:/a.txt");
			int ch = fileReader.read();
			System.out.println((char)ch);
			int ch1 = fileReader.read();
			System.out.println((char)ch1);
			int ch2 = fileReader.read();
			System.out.println((char)ch2);
			int ch3 = fileReader.read();
			System.out.println(ch3);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	@Test
	public void test() {
		try {
			FileReader fileReader = new FileReader("b.txt");
			int ch = -1;
			while ((ch = fileReader.read()) != -1) {
				System.out.println((char)ch);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	@Test
	public void	 test1() {
		try {
			FileReader fileReader = new FileReader("b.txt");
			int ch = -1;
			while ((ch = fileReader.read()) != -1) {//循环读入
				System.out.print((char)ch);
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	@Test
	public void test2() {
		try {
			FileReader fileReader = new FileReader("b.txt");
			char[] buffer = new char[10];
			int len = 0;
			while ((len = fileReader.read(buffer)) != -1) {
				System.out.println();
			}
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	@Test//字符流
	public void tets3() {
		FileReader fileReader = null;
		FileWriter fileWriter = null;
		try {
			fileReader = new FileReader("D:/a.txt");
			fileWriter = new FileWriter("D:/copy.txt");
			char[] buffer = new char[10];
			int len = 0;
			while ((len = fileReader.read(buffer)) != -1) {
				fileWriter.write(buffer, 0, len);
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			if (fileWriter != null) {
				try {
					fileWriter.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				if (fileReader != null) {
					try {
						fileWriter.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}
	}
	
	@Test//字节流图片
	public void test4() {
		FileInputStream fileInputStream = null;
		FileOutputStream fileOutputStream = null;
		try {
			fileInputStream = new FileInputStream("D:/捕获.png");
			fileOutputStream = new FileOutputStream("D:/copy.png");
			byte[] buffer = new byte[1024];
			int len = 0;
			while ((len = fileInputStream.read(buffer))!= -1) {
				fileOutputStream.write(buffer, 0, len);
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			if (fileInputStream != null) {
				try {
					fileInputStream.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				if (fileOutputStream != null) {
					try {
						fileOutputStream.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}
	}
}
